package com.example.flowermall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.flowermall.entity.IntegralProduct;
import com.example.flowermall.entity.Product;
import com.example.flowermall.entity.ProductImg;
import com.example.flowermall.mapper.IntegralProductMapper;
import com.example.flowermall.service.IProductImgService;
import com.example.flowermall.service.IntegralProductService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (IntegralProduct)表服务实现类
 *
 * @author mq
 * @since 2025-02-18 15:24:41
 */
@Service("integralProductService")
public class IntegralProductServiceImpl extends ServiceImpl<IntegralProductMapper,IntegralProduct> implements IntegralProductService {
    @Resource
    private IntegralProductMapper integralProductMapper;
    private final IProductImgService productImgService;

    public IntegralProductServiceImpl(IProductImgService productImgService) {
        this.productImgService = productImgService;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param productId 主键
     * @return 实例对象
     */
    @Override
    public IntegralProduct queryById(Integer productId) {
        return this.integralProductMapper.queryById(productId);
    }

    /**
     * 新增数据
     *
     * @param integralProduct 实例对象
     * @return 实例对象
     */
    @Override
    public IntegralProduct insert(IntegralProduct integralProduct) {
        this.integralProductMapper.insert(integralProduct);
        List<ProductImg> productImgList = integralProduct.getProductImgList();
        insertImg(productImgList,integralProduct);
        return integralProduct;
    }

    /**
     * 修改数据
     *
     * @param integralProduct 实例对象
     * @return 实例对象
     */
    @Override
    public IntegralProduct update(IntegralProduct integralProduct) {
        productImgService.deleteByIntegralProductId(integralProduct.getProductId());
        List<ProductImg> productImgList = integralProduct.getProductImgList();
        insertImg(productImgList,integralProduct);
        this.integralProductMapper.update(integralProduct);
        return this.queryById(integralProduct.getProductId());
    }

    void insertImg(List<ProductImg> productImgList,IntegralProduct integralProduct){
        if(!ObjectUtils.isEmpty(productImgList)){
            for(ProductImg productImg : productImgList){
                ProductImg img = new ProductImg();
                if(Objects.equals(integralProduct.getProductUrl(),productImg.getImgUrl())){
                    img.setMainImg(1);
                }
                img.setProductId(integralProduct.getProductId());
                img.setImgUrl(productImg.getImgUrl());
                img.setCreateTime(LocalDateTime.now());
                img.setIsIntegral(1);
                productImgService.add(img);
            }
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param productId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer productId) {
        return this.integralProductMapper.deleteById(productId) > 0;
    }
    @Override
    public IntegralProduct getGoodsById(Integer id) {
        IntegralProduct product = integralProductMapper.queryById(id);
        if(ObjectUtils.isEmpty(product)){
            return product;
        }
        ProductImg byProductId = productImgService.getByProductId(product.getProductId());
        if(byProductId!=null){
            product.setProductUrl(byProductId.getImgUrl());
        }
        return product;
    }

    @Override
    public List<IntegralProduct> getList() {
        return null;
    }
}
